--    Program: 		CAPPOIMP_ATTACHMENT_DOCUMENT_B.sql
--    Description:  Import PO Attached Document information 
--    Revision:     1st     
--    Parameter: 	
--    Created By:   Francis Lee
--    Date:         27-Jan-2006

set doc off;      
set verify off;
set serveroutput on size 1000000;
whenever sqlerror exit failure rollback;

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
   l_err_msg_tmp			varchar2(2000);
   
   l_document_id  			number;
                    		                 		
   cursor cur_attachment is
      select * from CRST_FND_DOCUMENTS for update;

begin
   l_processed_count := 0;
   l_rejected_count  := 0;
   l_err_msg := '';
   
   for cur in cur_attachment loop  
   	 l_err_msg_tmp := '';
   	 
   	 SELECT FND_DOCUMENTS_S.NEXTVAL 
     INTO l_document_id
     FROM DUAL;
     
     update CRST_FND_DOCUMENTS
     set    document_id_n = l_document_id
     where  document_id = cur.DOCUMENT_ID;
     
     begin
       -- import data
       insert into FND_DOCUMENTS
       (
         DOCUMENT_ID             ,
         CREATION_DATE           ,
         CREATED_BY              ,
         LAST_UPDATE_DATE        ,
         LAST_UPDATED_BY         ,
         LAST_UPDATE_LOGIN       ,
         DATATYPE_ID             ,
         CATEGORY_ID             ,
         SECURITY_TYPE           ,
         SECURITY_ID             ,
         PUBLISH_FLAG            ,
         IMAGE_TYPE              ,
         STORAGE_TYPE            ,
         USAGE_TYPE              ,
         START_DATE_ACTIVE       ,
         END_DATE_ACTIVE         ,
         REQUEST_ID              ,
         PROGRAM_APPLICATION_ID  ,
         PROGRAM_ID              ,
         PROGRAM_UPDATE_DATE     ,
         APP_SOURCE_VERSION      ,
         DM_NODE                 ,
         DM_FOLDER_PATH          ,
         DM_TYPE                 ,
         DM_DOCUMENT_ID          ,
         DM_VERSION_NUMBER       
       )
       values
       (
         l_document_id                 ,
         cur.CREATION_DATE             ,
         CAPBRIMP_MAPPING.GetUserMapping(cur.CREATED_BY),
         cur.LAST_UPDATE_DATE          ,
         CUR.LAST_UPDATED_BY,
         cur.LAST_UPDATE_LOGIN         ,
         cur.DATATYPE_ID               ,
         cur.CATEGORY_ID               ,
         cur.SECURITY_TYPE             ,
         cur.SECURITY_ID               ,
         cur.PUBLISH_FLAG              ,
         cur.IMAGE_TYPE                ,
         cur.STORAGE_TYPE              ,
         cur.USAGE_TYPE                ,
         cur.START_DATE_ACTIVE         ,
         cur.END_DATE_ACTIVE           ,
         cur.REQUEST_ID                ,
         cur.PROGRAM_APPLICATION_ID    ,
         cur.PROGRAM_ID                ,
         cur.PROGRAM_UPDATE_DATE       ,
         cur.APP_SOURCE_VERSION        ,
         cur.DM_NODE                   ,
         cur.DM_FOLDER_PATH            ,
         cur.DM_TYPE                   ,
         cur.DM_DOCUMENT_ID            ,
         cur.DM_VERSION_NUMBER
       );
     exception
       when others then
     	 l_err_msg_tmp := sqlerrm;
     end;
     
	 if l_err_msg_tmp IS NULL then
       l_processed_count := l_processed_count + 1;  
     else
       l_rejected_count := l_rejected_count + 1;
       l_err_msg := l_err_msg_tmp; 
     end if;  
   end loop;

   if l_rejected_count = 0 then
   	 commit;
   else
     rollback;
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPPOIMP_ATTACHMENT_DOCUMENT_B', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/


--exit
